#include "ntc.h"
#include <rtthread.h>
ADC_HandleTypeDef ADC_Handle;
#define BUFFER_LENGTH    (21)
uint32_t Vref;
uint32_t gu32_AdcBuffer[BUFFER_LENGTH];
struct
{

    int32_t temp;
    float res;
}ntc_tab[] = {
#include "ntc.txt"
};
uint32_t ADC_GetVrefP(uint8_t Pre_Channel)
{
    ADC_ChannelConfTypeDef ADC_ChannelConf;
    uint32_t TrimValue_3v, AdcValue_VrefP[2], VrefP,temp;
    
    ADC_Handle.Init.ClockDiv = ADC_CLOCK_DIV8;
    ADC_Handle.Init.ConConvMode = ADC_CONCONVMODE_DISABLE;
    ADC_Handle.Init.JChannelMode = ADC_JCHANNELMODE_DISABLE;
    ADC_Handle.Init.DiffMode = ADC_DIFFMODE_DISABLE;
    ADC_Handle.Init.DMAMode = ADC_DMAMODE_DISABLE;
    ADC_Handle.Init.OverMode = ADC_OVERMODE_DISABLE;
    ADC_Handle.Init.OverSampMode = ADC_OVERSAMPMODE_DISABLE;
    ADC_Handle.Init.AnalogWDGEn = ADC_ANALOGWDGEN_DISABLE;
    ADC_Handle.Init.ExTrigMode.ExTrigSel = ADC_SOFTWARE_START;
    ADC_Handle.Init.ChannelEn = ADC_CHANNEL_VBGR_EN;

    ADC_Handle.Instance = ADC;
        
    HAL_ADC_Init(&ADC_Handle);

    /* The total adc regular channels number */
    ADC_Handle.ChannelNum = 2;
    
     /* Add adc channels */
    ADC_ChannelConf.Channel = Pre_Channel;
    ADC_ChannelConf.RjMode = 0;
    ADC_ChannelConf.Sq = ADC_SEQUENCE_SQ1;
    ADC_ChannelConf.Smp = ADC_SMP_CLOCK_320;    
    HAL_ADC_ConfigChannel(&ADC_Handle,&ADC_ChannelConf);
    
    /* Add adc channels */
    ADC_ChannelConf.Channel = ADC_CHANNEL_VBGR;
    ADC_ChannelConf.RjMode = 0;
    ADC_ChannelConf.Sq = ADC_SEQUENCE_SQ2;
    ADC_ChannelConf.Smp = ADC_SMP_CLOCK_320;    
    HAL_ADC_ConfigChannel(&ADC_Handle,&ADC_ChannelConf);
    
    HAL_ADC_Polling(&ADC_Handle, AdcValue_VrefP, ADC_Handle.ChannelNum, 0);
    
    rt_kprintf("The adc convert result : Channel 18 = 0x%08x \r\n", AdcValue_VrefP[1]);
    
    TrimValue_3v = *(volatile uint32_t*)(0x00080240); //Read the 1.2v trim value in 3.0v vrefp.

    rt_kprintf("The adc 1.2v trim value is : 0x%08x \r\n", TrimValue_3v);

    if(((~TrimValue_3v&0xFFFF0000)>>16) == (TrimValue_3v&0x0000FFFF))
    {
        temp = TrimValue_3v & 0xFFF;
        
        VrefP = (uint32_t)(temp * 3000 / (AdcValue_VrefP[1] & 0xFFF));
                
        return VrefP;
    }
    else
        return 0;
}
void ADC_Init_Polling_Nchannels(void)
{
    ADC_ChannelConfTypeDef ADC_ChannelConf;
    
    ADC_Handle.Init.ClockDiv = ADC_CLOCK_DIV8;
    ADC_Handle.Init.ConConvMode = ADC_CONCONVMODE_DISABLE;
    ADC_Handle.Init.JChannelMode = ADC_JCHANNELMODE_DISABLE;
    ADC_Handle.Init.DiffMode = ADC_DIFFMODE_DISABLE;
    ADC_Handle.Init.DMAMode = ADC_DMAMODE_DISABLE;
    ADC_Handle.Init.OverMode = ADC_OVERMODE_DISABLE;
    ADC_Handle.Init.OverSampMode = ADC_OVERSAMPMODE_DISABLE;
    ADC_Handle.Init.AnalogWDGEn = ADC_ANALOGWDGEN_DISABLE;
    ADC_Handle.Init.ExTrigMode.ExTrigSel = ADC_SOFTWARE_START;
    ADC_Handle.Init.ChannelEn = ADC_CHANNEL_12_EN | ADC_CHANNEL_13_EN ;

    ADC_Handle.Instance = ADC;
        
    HAL_ADC_Init(&ADC_Handle);

    /* The total adc regular channels number */
    ADC_Handle.ChannelNum = 2;
    
    /* Add adc channels */
    ADC_ChannelConf.Channel = ADC_CHANNEL_12;
    ADC_ChannelConf.RjMode = 0;
    ADC_ChannelConf.Sq = ADC_SEQUENCE_SQ1;
    ADC_ChannelConf.Smp = ADC_SMP_CLOCK_320;    
    HAL_ADC_ConfigChannel(&ADC_Handle,&ADC_ChannelConf);
    
    ADC_ChannelConf.Channel = ADC_CHANNEL_13;
    ADC_ChannelConf.RjMode = 0;
    ADC_ChannelConf.Sq = ADC_SEQUENCE_SQ2;
    ADC_ChannelConf.Smp = ADC_SMP_CLOCK_320;   
    HAL_ADC_ConfigChannel(&ADC_Handle,&ADC_ChannelConf);
}
int32_t ntp_get_temp(int index)
{
		uint32_t i,j, Voltage;
    float res[2];
    int32_t temp[2];
    if(index != 0 && index != 1)
				return  -1;
	
    HAL_ADC_Polling(&ADC_Handle, gu32_AdcBuffer, ADC_Handle.ChannelNum, 0);
       
    for (i = 0; i < ADC_Handle.ChannelNum; i++)
    {
        Voltage = (gu32_AdcBuffer[i]&0xFFF)*Vref/4095;
        res[i] = 10.0 * Voltage / (Vref - Voltage);
        for (j = 0; j < sizeof(ntc_tab) / sizeof(ntc_tab[0]); j++)
        {
            if ((res[i] > ntc_tab[j].res) &&
                (res[i] < ntc_tab[j - 1].res))
            {
                temp[i] = ntc_tab[j].temp;
                
                break;
            }
        }
    }
    rt_kprintf("temp12:%d, temp13:%d\n", temp[0], temp[1]);
		return temp[index];
}
int adc_init(void)
{
	Vref = ADC_GetVrefP(ADC_CHANNEL_8);
	ADC_Init_Polling_Nchannels();
	return 0;
}
INIT_BOARD_EXPORT(adc_init);

